home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / box_cursor.pro < prev    next >
Text File  |  1997-07-08  |  4KB  |  167 lines

  1. ; $Id: box_cursor.pro,v 1.4 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1990-1997, Research Systems, Inc.  All rights reserved.
  4. ;    Unauthorized reproduction prohibited.
  5. ;
  6.  
  7. pro box_cursor, x0, y0, nx, ny, INIT = init, FIXED_SIZE = fixed_size, $
  8.     MESSAGE = message
  9. ;+
  10. ; NAME:
  11. ;    BOX_CURSOR
  12. ;
  13. ; PURPOSE:
  14. ;    Emulate the operation of a variable-sized box cursor (also known as
  15. ;    a "marquee" selector).
  16. ;
  17. ; CATEGORY:
  18. ;    Interactive graphics.
  19. ;
  20. ; CALLING SEQUENCE:
  21. ;    BOX_CURSOR, x0, y0, nx, ny [, INIT = init] [, FIXED_SIZE = fixed_size]
  22. ;
  23. ; INPUTS:
  24. ;    No required input parameters.
  25. ;
  26. ; OPTIONAL INPUT PARAMETERS:
  27. ;    x0, y0, nx, and ny give the initial location (x0, y0) and 
  28. ;    size (nx, ny) of the box if the keyword INIT is set.  Otherwise, the 
  29. ;    box is initially drawn in the center of the screen.
  30. ;
  31. ; KEYWORD PARAMETERS:
  32. ;    INIT:  If this keyword is set, x0, y0, nx, and ny contain the initial
  33. ;    parameters for the box.
  34. ;
  35. ;    FIXED_SIZE:  If this keyword is set, nx and ny contain the initial
  36. ;    size of the box.  This size may not be changed by the user.
  37. ;
  38. ;    MESSAGE:  If this keyword is set, print a short message describing
  39. ;    operation of the cursor.
  40. ;
  41. ; OUTPUTS:
  42. ;    x0:  X value of lower left corner of box.
  43. ;    y0:  Y value of lower left corner of box.
  44. ;    nx:  width of box in pixels.
  45. ;    ny:  height of box in pixels. 
  46. ;
  47. ;    The box is also constrained to lie entirely within the window.
  48. ;
  49. ; COMMON BLOCKS:
  50. ;    None.
  51. ;
  52. ; SIDE EFFECTS:
  53. ;    A box is drawn in the currently active window.  It is erased
  54. ;    on exit.
  55. ;
  56. ; RESTRICTIONS:
  57. ;    Works only with window system drivers.
  58. ;
  59. ; PROCEDURE:
  60. ;    The graphics function is set to 6 for eXclusive OR.  This
  61. ;    allows the box to be drawn and erased without disturbing the
  62. ;    contents of the window.
  63. ;
  64. ;    Operation is as follows:
  65. ;    Left mouse button:   Move the box by dragging.
  66. ;    Middle mouse button: Resize the box by dragging.  The corner
  67. ;        nearest the initial mouse position is moved.
  68. ;    Right mouse button:  Exit this procedure, returning the 
  69. ;                 current box parameters.
  70. ;
  71. ; MODIFICATION HISTORY:
  72. ;    DMS, April, 1990.
  73. ;    DMS, April, 1992.  Made dragging more intutitive.
  74. ;    June, 1993 - Bill Thompson
  75. ;            prevented the box from having a negative size.
  76. ;-
  77.  
  78. device, get_graphics = old, set_graphics = 6  ;Set xor
  79. col = !d.n_colors -1
  80.  
  81. if keyword_set(message) then begin
  82.     print, "Drag Left button to move box."
  83.     print, "Drag Middle button near a corner to resize box."
  84.     print, "Right button when done."
  85.     endif
  86.  
  87. if keyword_set(init) eq 0 then begin  ;Supply default values for box:
  88.     if keyword_set(fixed_size) eq 0 then begin
  89.         nx = !d.x_size/8   ;no fixed size.
  90.         ny = !d.x_size/8
  91.         endif
  92.     x0 = !d.x_size/2 - nx/2
  93.     y0 = !d.y_size/2 - ny/2
  94.     endif
  95.  
  96. button = 0
  97. goto, middle
  98.  
  99. while 1 do begin
  100.     old_button = button
  101.     cursor, x, y, 2, /dev    ;Wait for a button
  102.     button = !err
  103.     if (old_button eq 0) and (button ne 0) then begin
  104.         mx0 = x        ;For dragging, mouse locn...
  105.         my0 = y        
  106.         x00 = x0    ;Orig start of ll corner
  107.         y00 = y0
  108.         endif
  109.     if !err eq 1 then begin  ;Drag entire box?
  110.         x0 = x00 + x - mx0
  111.         y0 = y00 + y - my0
  112.         endif
  113.     if (!err eq 2) and (keyword_set(fixed_size) eq 0) then begin ;New size?
  114.         if old_button eq 0 then begin    ;Find closest corner
  115.             mind = 1e6
  116.             for i=0,3 do begin
  117.                 d = float(px[i]-x)^2 + float(py[i]-y)^2
  118.                 if d lt mind then begin
  119.                     mind = d
  120.                     corner = i
  121.                     endif
  122.                endfor
  123.             nx0 = nx    ;Save sizes.
  124.                ny0 = ny
  125.             endif
  126.         dx = x - mx0 & dy = y - my0    ;Distance dragged...
  127.         case corner of
  128.         0: begin x0 = x00 + dx & y0 = y00 + dy
  129.             nx = nx0 -dx & ny = ny0 - dy & endcase
  130.         1: begin y0 = y00 + dy
  131.             nx = nx0 + dx & ny = ny0 - dy & endcase
  132.         2: begin nx = nx0 + dx & ny = ny0 + dy & endcase
  133.         3: begin x0 = x00 + dx
  134.             nx = nx0 -  dx & ny = ny0 + dy & endcase
  135.         endcase
  136.         endif
  137.     plots, px, py, col=col, /dev, thick=1, lines=0    ;Erase previous box
  138.     empty                ;Decwindow bug
  139.  
  140.     if !err eq 4 then begin  ;Quitting?
  141.         device,set_graphics = old
  142.         return
  143.         endif
  144. middle:
  145.  
  146.     if nx lt 0 then begin
  147.         x0 = x0 + nx
  148.         nx = -nx
  149.     endif
  150.     if ny lt 0 then begin
  151.         y0 = y0 + ny
  152.         ny = -ny
  153.     endif
  154.  
  155.     x0 = x0 > 0
  156.     y0 = y0 > 0
  157.     x0 = x0 < (!d.x_size-1 - nx)    ;Never outside window
  158.     y0 = y0 < (!d.y_size-1 - ny)
  159.  
  160.     px = [x0, x0 + nx, x0 + nx, x0, x0] ;X points
  161.     py = [y0, y0, y0 + ny, y0 + ny, y0] ;Y values
  162.  
  163.     plots,px, py, col=col, /dev, thick=1, lines=0  ;Draw the box
  164.     wait, .1        ;Dont hog it all
  165.     endwhile
  166. end
  167.